﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace SAR
{
	public partial class TestPage7
	{
		public TestPage7()
		{
			this.InitializeComponent();

			// 在此点之下插入创建对象所需的代码。
		}

        private Bitmap bitmap1;
        private Bitmap bitmap2;
        private Bitmap bitmap3;

        private void btn1_SARBigBtnClick(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            string filename;
            if (open.ShowDialog() == DialogResult.OK)
            {
                filename = open.FileName;
                bitmap1 = new Bitmap(filename);
                sARImageSelector1.SARImageSource = getBitmapImage(bitmap1);
            }
        }

        private void sARImageSelector1_SARImageSelect(double imgAreaWidth, double imgAreaHeight, System.Windows.Point selectionLeftTop, System.Windows.Point selectionRightBottom)
        {
            double newheight =(selectionRightBottom.Y-selectionLeftTop.Y) * bitmap1.Height  / imgAreaHeight;
            double newwidth = (selectionRightBottom.X-selectionLeftTop.X) * bitmap1.Width  / imgAreaWidth;
            double offsetx = selectionLeftTop.X * bitmap1.Width  / imgAreaWidth;
            double offsety = selectionLeftTop.Y*bitmap1.Height / imgAreaHeight;
            bitmap2 = getPartOfImage(bitmap1, r(newwidth), r(newheight), r(offsetx), r(offsety));
            sARImageSelector2.SARImageSource = getBitmapImage(bitmap2);
        }

        private void sARImageSelector2_SARImageSelect(double imgAreaWidth, double imgAreaHeight, System.Windows.Point selectionLeftTop, System.Windows.Point selectionRightBottom)
        {
            double newheight = (selectionRightBottom.Y - selectionLeftTop.Y) * bitmap2.Height / imgAreaHeight;
            double newwidth = (selectionRightBottom.X - selectionLeftTop.X) * bitmap2.Width / imgAreaWidth;
            double offsetx = selectionLeftTop.X * bitmap2.Width / imgAreaWidth;
            double offsety = selectionLeftTop.Y * bitmap2.Height / imgAreaHeight;
            bitmap3 = getPartOfImage(bitmap2, r(newwidth), r(newheight), r(offsetx), r(offsety));
            sARImageSelector3.SARImageSource = getBitmapImage(bitmap3);
            
        }

        private int r(double d)
        {
            return (int)Math.Round(d, MidpointRounding.AwayFromZero);
        }

        private void sARImageSelector3_SARImagePointSelect(double imgAreaWidth, double imgAreaHeight, System.Windows.Point selectionPoint)
        {
            System.Windows.MessageBox.Show(String.Format("您选择了{0},{1}",selectionPoint.X,selectionPoint.Y));
        }

        private BitmapSource getBitmapImage(Bitmap m_Bitmap)
        {
            IntPtr ip = m_Bitmap.GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ip, IntPtr.Zero, Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(ip);
            return bitmapSource;
        }

        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        public Bitmap getPartOfImage(Bitmap originalBitmap, int width, int height, int offsetX, int offsetY)
        {
            Bitmap resultBitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(resultBitmap))
            {
                System.Drawing.Rectangle resultRectangle = new System.Drawing.Rectangle(0, 0, width, height);
                System.Drawing.Rectangle sourceRectangle = new System.Drawing.Rectangle(0 + offsetX, 0 + offsetY, width, height);
                g.DrawImage(originalBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
            }
            return resultBitmap;
        }


        ////////////////////////////////////////////////////
        //////////////////////////////////////////////
        //下面是测试时间控件的

        /// <summary>
        /// /////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void sAR_BigButton1_SARBigBtnClick(object sender, MouseButtonEventArgs e)
        {
            dateTimePicker1.SelectedDateTime = new DateTime(2005, 5, 4, 13, 4, 5);
        } 
	}    
}